package com.example.arcgisdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.Toast;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.data.GeoPackage;
import com.esri.arcgisruntime.data.GeoPackageFeatureTable;
import com.esri.arcgisruntime.data.Geodatabase;
import com.esri.arcgisruntime.data.GeodatabaseFeatureTable;
import com.esri.arcgisruntime.data.TileCache;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.MobileMapPackage;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.MapView;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private final static String TAG = MainActivity.class.getSimpleName();

    private MapView mMapView;
    private TileCache tileCache;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArcGISRuntimeEnvironment.setApiKey("AAPK16f373c3c78b4649a69e3a835c5d3882R5BVdgvKpdtCoQiyCZ2tmmmDBdceVK9Gz2duNf4Fy1Q4Uo-LUbfAvSR6NuKBOr2I");

        // inflate MapView from layout
        mMapView = findViewById(R.id.mapView);
        // create a map with the a topographic basemap
        ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
        // set the map to be displayed in this view
        mMapView.setMap(map);
        mMapView.setViewpoint(new Viewpoint(31.734, 119.315, 10000));

        TileCache tileCache=new TileCache(Environment.getExternalStorageDirectory()+ File.separator + getString(R.string.geopackage_path));
        ArcGISTiledLayer tiledLayer = new ArcGISTiledLayer(tileCache);
        Basemap basemap = new Basemap(tiledLayer);
////
        ArcGISMap mMap = new ArcGISMap(basemap);
////        // set the map to be displayed in the Map View
        mMapView.setMap(mMap);
//        mMapView.setViewpoint(new Viewpoint(30.457572, 114.125678, 10000));
//        mMapView.setOnTouchListener(new MapView.OnTouchListener() {
//            @Override
//            public boolean onMultiPointerTap(MotionEvent motionEvent) {
//                return false;
//            }
//
//            @Override
//            public boolean onDoubleTouchDrag(MotionEvent motionEvent) {
//                return false;
//            }
//
//            @Override
//            public boolean onUp(MotionEvent motionEvent) {
//                return false;
//            }
//
//            @Override
//            public boolean onRotate(MotionEvent motionEvent, double v) {
//                return false;
//            }
//
//            @Override
//            public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
//                return false;
//            }
//
//            @Override
//            public boolean onDoubleTap(MotionEvent motionEvent) {
//                return false;
//            }
//
//            @Override
//            public boolean onDoubleTapEvent(MotionEvent motionEvent) {
//                return false;
//            }
//
//            @Override
//            public boolean onDown(MotionEvent motionEvent) {
//                return false;
//            }
//
//            @Override
//            public void onShowPress(MotionEvent motionEvent) {
//
//            }
//
//            @Override
//            public boolean onSingleTapUp(MotionEvent motionEvent) {
//                Log.e("ARCGIS","单机地图");
//                return false;
//            }
//
//            @Override
//            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
//                return false;
//            }
//
//            @Override
//            public void onLongPress(MotionEvent motionEvent) {
//
//            }
//
//            @Override
//            public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
//                return false;
//            }
//
//            @Override
//            public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
//                return false;
//            }
//
//            @Override
//            public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
//                return false;
//            }
//
//            @Override
//            public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {
//
//            }
//
//            @Override
//            public boolean onTouch(View view, MotionEvent motionEvent) {
//                return false;
//            }
//        });
//        tileCache.loadAsync();
//        tileCache.addDoneLoadingListener(()->{
//            if (tileCache.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
//                String error = "tileCache failed to load: " + tileCache.getLoadError();
//                Log.e(TAG, error);
//                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
//                return;
//            }
//
//
//            }
//        );

//        mGeoPackage = new GeoPackage(Environment.getExternalStorageDirectory()+ File.separator + getString(R.string.geopackage_path));
//        加载geodatabase
        Geodatabase geodatabase=new Geodatabase(Environment.getExternalStorageDirectory()+ File.separator + getString(R.string.geopackage_path1));
        geodatabase.loadAsync();
        geodatabase.addDoneLoadingListener(()->{
            if (geodatabase.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
                String error = "Geopackage failed to load: " + geodatabase.getLoadError();
                Log.e(TAG, error);
                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                return;
            }else {
                for (GeodatabaseFeatureTable geoPackageFeatureTable : geodatabase.getGeodatabaseFeatureTables()) {
//                // create a FeatureLayer from the GeoPackageFeatureLayer
                FeatureLayer featureLayer = new FeatureLayer(geoPackageFeatureTable);

                // add the layer to the map
                mMapView.getMap().getOperationalLayers().add(featureLayer);
            }
            }
            }
        );
        //加载mmpk
//        mobileMapPackage = new MobileMapPackage(Environment.getExternalStorageDirectory()+ File.separator + getString(R.string.geopackage_path));
//
//
//        mobileMapPackage.loadAsync();
//        mobileMapPackage.addDoneLoadingListener(() -> {
//            if (mobileMapPackage.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
//                String error = "Geopackage failed to load: " + mobileMapPackage.getLoadError();
//                Log.e(TAG, error);
//                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
//                return;
//            }
//            List<ArcGISMap> maps = mobileMapPackage.getMaps();
//            ArcGISMap arcGISMap = maps.get(0); //地图里包含了的一个底图层和两个业务图层
//            mMapView.setMap(arcGISMap);
//        });
        //加载geopakage
//        mGeoPackage.loadAsync();
//        mGeoPackage.addDoneLoadingListener(() -> {
//            if (mGeoPackage.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
//                String error = "Geopackage failed to load: " + mGeoPackage.getLoadError();
//                Log.e(TAG, error);
//                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
//                return;
//            }
//
//            // loop through each GeoPackageRaster
//            for (GeoPackageFeatureTable geoPackageRaster : mGeoPackage.getGeoPackageFeatureTables()) {
////                for (GeoPackageRaster geoPackageRaster : mGeoPackage.getGeoPackageRasters()) {//读取栅格图层
//                // create a RasterLayer from the GeoPackageRaster
//                FeatureLayer featureLayer = new FeatureLayer(geoPackageRaster);
//
//                // add the layer to the map
//                mMapView.getMap().getOperationalLayers().add(featureLayer);
//            }
//
//            // loop through each GeoPackageFeatureTable
//            for (GeoPackageFeatureTable geoPackageFeatureTable : mGeoPackage.getGeoPackageFeatureTables()) {
//                // create a FeatureLayer from the GeoPackageFeatureLayer
//                FeatureLayer featureLayer = new FeatureLayer(geoPackageFeatureTable);
//
//                // add the layer to the map
//                mMapView.getMap().getOperationalLayers().add(featureLayer);
//            }
//        });
    }


    @Override
    protected void onStart() {
        super.onStart();



    }

}